const fs = require("fs");
const querystring = require("querystring");
const userController = require("./controller");

module.exports = async (req, res) => {
  if (req.url === "/") {
    if (req.method === "GET") {
      userController.index(res);
    } else {
      let data;
      req.on("data", (chunk) => {
        console.log("Received data chunk:", chunk.toString());
        data += chunk.toString();
      });

      req.on("end", () => {
        userController.user(querystring.parse(data), res);
      });
    }
  } else if (/.\.png/.test(req.url)) {
    res.writeHead(200, { "Content-Type": "image/png" });
    const data = await fs.promises.readFile("monica.png");
    res.end(data);
  } else if (req.url === "/submit") {
    console.log("body", JSON.stringify(req.body));
  }
};
